<?php

namespace common\repositories\outstock;

use common\entities\outstock\MyscrmDeliveryFlowRecordEntity;
use common\repositories\BaseRepository;

/**
 * Created by PhpStorm.
 * User: luoy03
 * Date: 16-12-12
 * Time: 上午11:00
 */
class DeliveryFlowRecordRepository extends BaseRepository
{
    const STATUS_YDH= 10; //已打回
    const STATUS_YSP = 20;//已审批
    const STATUS_YFQ = 25;//发起中，针对特批授权
    const STATUS_YQR = 30;//已确认
    const STATUS_YKT = 40;//已开通
    const STATUS_YSQ = 50;//已授权

    public static $businessPatternMap=['联营'=>2,'直属'=>1];
    public function addRecord($data){
        $entity=new MyscrmDeliveryFlowRecordEntity();
        $entity->load($data,'');
        $saveRes=$entity->save();
        if($saveRes===false){
            return self::setAndReturn(__LINE__,$entity->getAllFirstErrorMessage());
        }
        return $saveRes;
    }
    public function getOrderRecord($deliveryGuid,$actionList=[10,15,20,21]){
        return MyscrmDeliveryFlowRecordEntity::find()
            ->where(['is_success'=>1,'delivery_guid'=>$deliveryGuid])
            ->andWhere(['in','action',$actionList])
            ->orderBy('created_on desc')
            ->asArray(true)
            ->all();
    }
    public function getSubOrderRecord($subOrderId,$actionList=[22,23]){
        return MyscrmDeliveryFlowRecordEntity::find()
            ->where(['is_success'=>1,'sub_order_id'=>$subOrderId])
            ->andWhere(['in','action',$actionList])
            ->orderBy('created_on desc')
            ->asArray(true)
            ->all();
    }
    public function getFlowRecordsBySubOrderIds($subOrderIds,$action,$fields='*'){
        return MyscrmDeliveryFlowRecordEntity::find()
            ->select($fields)
            ->where(['in','sub_order_id',$subOrderIds])
            ->andWhere(['action'=>$action])
            ->andWhere(['is_success'=>1])
            ->asArray(true)
            ->all();
    }
    public function getFlowRecordBySubOrderId($subOrderId,$action,$fields='*'){
        return MyscrmDeliveryFlowRecordEntity::find()
            ->select($fields)
            ->where(['sub_order_id'=>$subOrderId])
            ->andWhere(['action'=>$action])
            ->andWhere(['is_success'=>1])
            ->asArray(true)
            ->all();
    }
}